// var fs = require('fs');
const express = require('express')
const bodyParser = require('body-parser')
const cors = require('cors')
const app = express()
const axios = require('axios')

app.use(cors())
app.use(bodyParser.urlencoded({ extended: false }))
app.use(bodyParser.json({limit: '50mb'}))
// const multipart = require('connect-multiparty')
// const multipartyMiddleware = multipart()

app.get('/abc', function(req, res) {
    console.log('abc')
    const url = 'https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=sAz059dji21lgdw4tNc0jjCf&client_secret=vWq9Dg0N9BmnswNpD7LAspTMh8l2buCg'
    axios({
        url,
        method:'GET'
    }).then((rsp) => {
        res.send(rsp.data)
    })
})

app.post('/baiduAi', function(req, res) {
    const url = 'https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=sAz059dji21lgdw4tNc0jjCf&client_secret=vWq9Dg0N9BmnswNpD7LAspTMh8l2buCg'
    axios({
        url,
        method:'GET'
    }).then((rsp) => {
        res.send(rsp.data)
    })
})

app.post('/query', function(req, res) {
    const wbody = req.body;
    const { question, access_token } = wbody
    console.log(question, 'question')
    const url = 'https://aip.baidubce.com/rpc/2.0/ai_custom/v1/wenxinworkshop/chat/completions_pro?access_token=' + access_token
    const headers = {
        'Content-Type': 'application/json'
    }
    axios({
        url,
        method:'POST',
        headers,
        data: {
            messages:[{
                role:'user',
                content: question
            }]
        }
    }).then((rsp) => {
        // console.log(rsp)
        res.send(rsp.data)
    })
})

app.post('/drawing', function(req, res) {
    const wbody = req.body;
    const { prompt, negativePrompt, size, steps, n, access_token } = wbody
    const url = 'https://aip.baidubce.com/rpc/2.0/ai_custom/v1/wenxinworkshop/text2image/sd_xl?access_token=' + access_token
    const headers = {
        'Content-Type': 'application/json'
    }
    axios({
        url,
        method:'POST',
        headers,
        data: {
            prompt,
            // negativePrompt,
            // size,
            // steps,
            // n
        }
    }).then((rsp) => {
        // console.log(rsp)
        res.send(rsp.data)
    })
})

app.post('/querylaw', function(req, res) {
    const wbody = req.body;
    const { prompt, negativePrompt, size, steps, n, access_token } = wbody
    const url = 'https://aip.baidubce.com/rpc/2.0/ai_custom/v1/wenxinworkshop/chat/chatlaw?access_token=' + access_token
    const headers = {
        'Content-Type': 'application/json'
    }
    axios({
        url,
        method:'POST',
        headers,
        data: {
            messages:[{
                role:'user',
                content: prompt
            }]
        }
    }).then((rsp) => {
        console.log(rsp)
        res.send(rsp.data)
    })
})

app.post('/generateCode', function(req, res) {
    const wbody = req.body;
    const { prompt, negativePrompt, size, steps, n, access_token } = wbody
    const url = 'https://aip.baidubce.com/rpc/2.0/ai_custom/v1/wenxinworkshop/completions/sqlcoder_7b?access_token=' + access_token
    const headers = {
        'Content-Type': 'application/json'
    }
    axios({
        url,
        method:'POST',
        headers,
        data: {
            prompt,
            // negativePrompt,
            // size,
            // steps,
            // n
        }
    }).then((rsp) => {
        // console.log(rsp)
        res.send(rsp.data)
    })
})

app.post('/queryFinance', function(req, res) {
    const wbody = req.body;
    const { prompt, negativePrompt, size, steps, n, access_token } = wbody
    const url = 'https://aip.baidubce.com/rpc/2.0/ai_custom/v1/wenxinworkshop/chat/xuanyuan_70b_chat?access_token=' + access_token
    const headers = {
        'Content-Type': 'application/json'
    }
    axios({
        url,
        method:'POST',
        headers,
        data: {
            messages:[
                {
                    role:'user',
                    content: prompt
                }
            ]
        }
    }).then((rsp) => {
        // console.log(rsp)
        res.send(rsp.data)
    })
})
   
//开端口
var server = app.listen(3001,function(){
    
    console.log('服务器在3001端口打开了');
})